<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="公司名称" prop="companyName">
        <el-input maxlength="50"
                  class="search-width-big"
                  v-model="queryParams.companyName"
                  placeholder="请输入公司名称"
                  clearable
                  @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会统一信用代码" prop="usci">
        <el-input maxlength="50"
            class="search-width-big"
          v-model="queryParams.usci"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:bizTrustee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:bizTrustee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:bizTrustee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:bizTrustee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bizTrusteeList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="序号" type="index" align="left" width="60" />
      <el-table-column label="公司名称" align="left" prop="companyName" min-width="250" show-overflow-tooltip/>
      <el-table-column label="进项税比例" align="left" prop="taxProportion" width="120">
        <template #default="scope">
        <span>{{scope.row.taxProportion*100}}%</span>
        </template>
      </el-table-column>
      <el-table-column label="社会统一信用代码" align="left" prop="usci" width="200" show-overflow-tooltip/>
      <el-table-column label="开户银行" align="left" prop="depositBank" width="180" show-overflow-tooltip/>
      <el-table-column label="开户行账号" align="left" prop="cardNumber" width="180" show-overflow-tooltip/>
      <el-table-column label="联系人" align="left" prop="contacts" width="120" show-overflow-tooltip/>
      <el-table-column label="联系电话" align="left" prop="phone" width="150" show-overflow-tooltip/>
      <el-table-column label="邮箱" align="left" prop="email" width="180" show-overflow-tooltip/>
      <el-table-column label="创建人" align="left" prop="userName" width="100"/>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180"/>
      <el-table-column fixed="right" label="操作" align="left" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:bizTrustee:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:bizTrustee:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改受托方管理对话框 -->
    <el-dialog :close-on-click-modal="false" draggable :title="title" v-model="open" width="800px" append-to-body @close="cancel">
      <el-form ref="bizTrusteeRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input maxlength="50" v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="进项税比例" prop="taxProportion">
          <PercentInput v-model="form.taxProportion" placeholder="请输入进项税比例" />
        </el-form-item>
        <el-form-item label="社会统一信用代码" prop="usci">
          <el-input maxlength="50" v-model="form.usci" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="开户银行" prop="depositBank">
          <el-input maxlength="50" v-model="form.depositBank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="开户行账号" prop="cardNumber">
          <el-input maxlength="50" v-model="form.cardNumber" placeholder="请输入开户行账号" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input maxlength="50" v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input maxlength="50" v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input maxlength="50" v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BizTrustee">
import { listBizTrustee, getBizTrustee, delBizTrustee, addBizTrustee, updateBizTrustee } from "@/api/biz/bizTrustee";
import PercentInput from '@/components/PercentInput.vue';
import {currentDate} from '@/utils/dateutil';

const { proxy } = getCurrentInstance();

const bizTrusteeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    contacts: null,
    phone: null,
    email: null,
    companyName: null,
    usci: null,
  },
  rules: {
    taxProportion: [
      { required: true, message: "进项税比例不能为空", trigger: "submit" }
    ],
    contacts: [
      { required: true, message: "联系人不能为空", trigger: "submit" }
    ],
    phone: [
      { required: true, message: "联系电话不能为空", trigger: "submit" }
    ],
    companyName: [
      { required: true, message: "公司名称不能为空", trigger: "submit" }
    ],
    usci: [
      { required: true, message: "社会统一信用代码不能为空", trigger: "submit" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询受托方管理列表 */
function getList() {
  loading.value = true;
  listBizTrustee(queryParams.value).then(response => {
    bizTrusteeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    updateTime: null,
    deleted: null,
    contacts: null,
    phone: null,
    email: null,
    companyName: null,
    taxProportion: null,
    usci: null,
    userId: null
  };
  proxy.resetForm("bizTrusteeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加受托方";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBizTrustee(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改受托方";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bizTrusteeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBizTrustee(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBizTrustee(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除受托方数据项？').then(function() {
    return delBizTrustee(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('biz/bizTrustee/export', {
    ...queryParams.value
  }, `受托方-${currentDate()}.xlsx`)
}

getList();
</script>
